package com.base.controller;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;

import com.base.service.SubscribersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import com.base.po.voip.Rewards;
import com.base.po.voip.Subscribers;
import com.base.po.voip.Users;
import com.base.service.RewardsService;
import com.base.service.UsersService;
import com.base.utils.Page;

/**
 * 任务信息查看服务端
 * 
 * @author f
 *
 */
@Controller
@RequestMapping("/admin")
public class index_Rewards {
	@Autowired
	private SubscribersService subscribersService;
	@Autowired
	private RewardsService rewardsService; // 任务信息
	@Autowired
	private UsersService usersService; // 任务信息

	/**
	 * 任务信息首页
	 * 
	 * @param request       获取参数
	 * @param currentPageNo 当前页
	 * @param mobile        手机号码
	 * @param appname       应用名称
	 * @param username      代理商名称
	 * @param shopname      商家名称
	 * @param createtime    任务时间
	 * @param type    		查询类型
	 * @return ModelAndView
	 * @throws Exception
	 */
	@RequestMapping("/shareRewards")
	public ModelAndView shareRewards(HttpServletRequest request, Integer currentPageNo, String userMobile, 
			String presentMobile, String presentName) throws Exception {
		String userid = (String) request.getSession().getAttribute("adminUserId"); // 登录的用户ID
		Integer adminUsergrade = (Integer) request.getSession().getAttribute("adminUsergrade");
		if (userid == null) return new ModelAndView("jsp/loginCheck"); // 未登录
		Map<String, Object> map = new HashMap<>(); // 存储数据对象
		// 设置分页 获取内容
		Page page = new Page(); // 实例化分页
		if (currentPageNo != null) page.setCurrentPageNo(currentPageNo); // 设置当前页 主页不设置
		List<Rewards> rewardsList = null;
		List<Users> presentUser = null;
		Subscribers userSub = null;
		Subscribers presentSub = null;
		String presentId = null;
		String userSubId = null;
		String presentSubId = null;
		if(presentName != null && !presentName.equals("") && presentName != "undefined") {
			presentUser = usersService.selectByUserName(presentName);
			if(presentUser != null && presentUser.size()>0) {
				presentId = presentUser.get(0).getId();
			}
		}
		if(userMobile != null && !userMobile.equals("") && userMobile != "undefined") {
			userSub = subscribersService.selectBySubscribers(userMobile, userid);
			if(userSub != null) {
				userSubId = userSub.getId();
			}
		}
		if(presentMobile != null && !presentMobile.equals("") && presentMobile != "undefined") {
			presentSub = subscribersService.selectBySubscribers(presentMobile, userid);
			if(presentSub != null) {
				presentSubId = presentSub.getId();
			}
		}	
		
		// 判断登录权限
		switch ((int) request.getSession().getAttribute("adminUsergrade")) {
		case 0: // ADMIN用户显示 全部
			page.setRecordTotal(rewardsService.countAll("1", presentId, userSubId, presentSubId)); // 设置总条数
			rewardsList = rewardsService.selectAll(page, "1", presentId, userSubId, presentSubId);
			break;
		case 1: // 代理商用户显示
			page.setRecordTotal(rewardsService.countByParentID(userid, "1", userSubId, presentSubId)); // 设置总条数
			rewardsList = rewardsService.selectByParentID(page, userid, "1", userSubId, presentSubId);
			break;
		}
		for(Rewards r : rewardsList) {
			Users presentU = usersService.selectByPrimaryKey(r.getParentid());
			if(presentU !=null) {
				r.setParentid(presentU.getUsername());
			}else {
				r.setParentid("未知代理商");
			}
			
			Subscribers userS = subscribersService.selectByPrimaryKey(r.getUserid());
			if(userS !=null) {
				r.setUserid(userS.getMobile());
			}else {
				r.setUserid("未知用户");
			}
			
			Subscribers presentS = subscribersService.selectByPrimaryKey(r.getPresentid());
			if(presentS !=null) {
				r.setPresentid(presentS.getMobile());
			}else {
				r.setPresentid("未知用户");
			}
			
			if(new BigDecimal("-1").compareTo(r.getPresentmoney()) == 0) {
				r.setMoneystatus("返利失败");
			}else {
				r.setMoneystatus(r.getPresentmoney().toString());
			}
			
			if(new BigDecimal("-1").compareTo(r.getPresentshopmoney()) == 0) {
				r.setShopmoneystatus("返利失败");
			}else {
				r.setShopmoneystatus(r.getPresentshopmoney().toString());
			}
		}
		map.put("rewardsList", rewardsList);
		map.put("userMobile", userMobile);
		map.put("presentMobile", presentMobile);
		map.put("presentName", presentName);
		map.put("adminUsergrade", adminUsergrade);
		map.put("pages", page); // 分页
		map.put("type", "1"); // 类型
		return new ModelAndView("jsp/index_Rewards", map);
	}
	
	@RequestMapping("/integralRewards")
	public ModelAndView integralRewards(HttpServletRequest request, Integer currentPageNo, String userMobile, 
			String presentMobile, String presentName) throws Exception {
		String userid = (String) request.getSession().getAttribute("adminUserId"); // 登录的用户ID
		Integer adminUsergrade = (Integer) request.getSession().getAttribute("adminUsergrade");
		if (userid == null) return new ModelAndView("jsp/loginCheck"); // 未登录
		Map<String, Object> map = new HashMap<>(); // 存储数据对象
		// 设置分页 获取内容
		Page page = new Page(); // 实例化分页
		if (currentPageNo != null) page.setCurrentPageNo(currentPageNo); // 设置当前页 主页不设置
		List<Rewards> rewardsList = null;
		List<Users> presentUser = null;
		Subscribers userSub = null;
		Subscribers presentSub = null;
		String presentId = null;
		String userSubId = null;
		String presentSubId = null;
		if(presentName != null && !presentName.equals("") && presentName != "undefined") {
			presentUser = usersService.selectByUserName(presentName);
			if(presentUser != null && presentUser.size()>0) {
				presentId = presentUser.get(0).getId();
			}
		}
		if(userMobile != null && !userMobile.equals("") && userMobile != "undefined") {
			userSub = subscribersService.selectBySubscribers(userMobile, userid);
			if(userSub != null) {
				userSubId = userSub.getId();
			}
		}
		if(presentMobile != null && !presentMobile.equals("") && presentMobile != "undefined") {
			presentSub = subscribersService.selectBySubscribers(presentMobile, userid);
			if(presentSub != null) {
				presentSubId = presentSub.getId();
			}
		}	
		
		// 判断登录权限
		switch ((int) request.getSession().getAttribute("adminUsergrade")) {
		case 0: // ADMIN用户显示 全部
			page.setRecordTotal(rewardsService.countAll("2", presentId, userSubId, presentSubId)); // 设置总条数
			rewardsList = rewardsService.selectAll(page, "2", presentId, userSubId, presentSubId);
			break;
		case 1: // 代理商用户显示
			page.setRecordTotal(rewardsService.countByParentID(userid, "2", userSubId, presentSubId)); // 设置总条数
			rewardsList = rewardsService.selectByParentID(page, userid, "2", userSubId, presentSubId);
			break;
		}
		for(Rewards r : rewardsList) {
			Users presentU = usersService.selectByPrimaryKey(r.getParentid());
			if(presentU !=null) {
				r.setParentid(presentU.getUsername());
			}
			Subscribers userS = subscribersService.selectByPrimaryKey(r.getUserid());
			if(userS !=null) {
				r.setUserid(userS.getMobile());
			}
			Subscribers presentS = subscribersService.selectByPrimaryKey(r.getPresentid());
			if(presentS !=null) {
				r.setPresentid(presentS.getMobile());
			}
		}
		map.put("rewardsList", rewardsList);
		map.put("userMobile", userMobile);
		map.put("presentMobile", presentMobile);
		map.put("presentName", presentName);
		map.put("adminUsergrade", adminUsergrade);
		map.put("pages", page); // 分页
		map.put("type", "2"); // 类型
		return new ModelAndView("jsp/index_Rewards", map);
	}
}
